package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC713 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int numSubarrayProductLessThanK(int[] arr, int k) {
            if (null == arr || arr.length <= 0) return 0;

            int left = 0, right = 0, count = arr[0] < k ? 1 : 0;
            long product = arr[0];
            while (right < arr.length - 1) {
                ++right;    //expand right boundary.
                product *= arr[right];

                if (product < k) {
                    count += right - left + 1;
                    continue;
                }

                while (product >= k && left < right) {
                    product /= arr[left++];
                }
                count += product >= k ? 0 : right - left + 1;
            }

            return count;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,1,1]");
        System.out.println(solution.numSubarrayProductLessThanK(arr, 0));
    }

    public static void main(String[] args) throws Exception {
        LC713 an = new LC713();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
